package com.lizardmind.haofudoctor.ui.aboutMe.set

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.blankj.utilcode.util.RegexUtils
import com.linxiao.framework.fragment.BaseFragment
import com.linxiao.framework.rx.RxJavaUtil
import com.linxiao.framework.util.getDataBinding
import com.linxiao.framework.util.intent
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.databinding.FragmrntChangePasswordPhoneBinding
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Consumer
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.support.v4.toast

/**
 * Created by Administrator on 2017/10/30.
 *  修改密码——根据手机号修改
 */
class ChangePasswordByPhoneNumFragment : BaseFragment() {

    val binding by lazy { getDataBinding<FragmrntChangePasswordPhoneBinding>(R.layout.fragmrnt_change_password_phone) }
    override fun onCreatRootView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return binding.root
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        binding.apply {
            phone=""
            verify=""
            time=0

            //获取验证码
            tvSeePassWordOld.onClick {
                if(RegexUtils.isMobileExact(phone)){
                    observable {
                        postApi<Request<String>>(hashMapOf("action" to "send_sms",
                                "mobile" to phone!!,"type" to "2")
                                , Consumer {
                            it.dispose {
                                toast("发送成功")
                                observable {
                                    RxJavaUtil.countDown(60).subscribe {
                                        time = it
                                    }
                                }

                            }
                        })
                    }
                }else{
                    toast("手机号码不正确")
                }
            }

            tvChangePasswordOK.setOnClickListener {
                observable {
                    postApi<Request<String>>(hashMapOf("action" to "doctor_edit_pword",
                            "type" to "2",
                            "userphone" to phone!!,
                            "verify" to verify!!)
                            , Consumer {
                        it.dispose {
                            toast("验证成功")
                            startActivity(intent<ChangePasswordActivity2>())
                            activity.finish()
                        }
                    })
                }

            }

        }


    }
}